package priv.yiyuan.example.design;

import java.io.IOException;

/**
 * 缓存装饰器
 *
 * @author YiYuan
 * @data 2025/8/8
 * @apoNote
 */
public class CachingDecorator extends DataReaderDecorator {

    private String cache;

    public CachingDecorator(DataReader wrappedReader) {
        super(wrappedReader);
    }

    @Override
    public String read() throws IOException {

        if (cache == null) {
            System.out.println("Cache miss - reading from source");
            cache = wrappedReader.read();
        }
        else {
            System.out.println("Cache hit - returning cached data");
        }

        return cache;
    }

    @Override
    public String getSourceInfo() {
        return "[Cached] " + wrappedReader.getSourceInfo();
    }
}
